<?php
namespace app\crm\controller;
use app\common\controller\UserBase; 
use app\crm\model\CrmCustomer;
use app\crm\model\CrmContract;
use app\crm\model\CrmTrack;
use app\crm\model\CrmSales;
use app\user\model\FieldData;
use app\crm\model\User;

class Analysis extends UserBase
{
    public function initialize()
    {
        parent::initialize();

    }
    //客户数据
    public function index()
    {   
        //时间 - 默认本月
        if(input('dateframe')){
            $dateframe = input('dateframe');
            $start_time = strtotime(substr($dateframe,0,10));
            $end_time = strtotime(substr($dateframe,-10))+86399;
        }else{
            $month = date('Y-m',time());
            $start_time = strtotime($month);//指定月份月初时间戳  
            $end_time = mktime(23, 59, 59, date('m', strtotime($month))+1, 00);//指定月份月末时间戳
            $dateframe = date('Y-m-d',$start_time).' - '.date('Y-m-d',$end_time);
        }
        //基本信息
        $data['customer'] = CrmCustomer::where('id','in', my_select_auth())->whereTime('create_time','between',[$start_time,$end_time])->count();
        $data['customer_all'] = CrmCustomer::where('id','in', my_select_auth())->count();
        $data['track'] = CrmTrack::where('customer_id','in', my_select_auth())->whereTime('create_time','between',[$start_time,$end_time])->count();
        $data['track_all'] = CrmTrack::where('customer_id','in', my_select_auth())->count();
        $data['order'] = CrmSales::where('customer_id','in', my_select_auth())->whereTime('create_time','between',[$start_time,$end_time])->sum('price');
        $data['order_count'] = CrmSales::where('customer_id','in', my_select_auth())->whereTime('create_time','between',[$start_time,$end_time])->count();
        $data['contract_count'] = CrmContract::where('customer_id','in', my_select_auth())->whereTime('create_time','between',[$start_time,$end_time])->count();
        //曲线图
        $now_j = (($end_time-$start_time)/86400);
        $num = '0';
        for($i=0;$i<$now_j;$i++){
            //每日时间戳
            $day_begin = $start_time+$i*86400;
            $day_end = ($start_time+$i*86400)+86399;
            
            $date[] = date('d',$start_time+$i*86400); //每隔一天赋值给数组
            $month_data['date'][] = date('m-d',$start_time+$i*86400); //每隔一天赋值给数组
            $month_data['price'][] = CrmSales::where('customer_id','in', my_select_auth())->whereTime('create_time', 'between', [$day_begin,$day_end])->sum('price');
        }
        
        return $this->fetch('index',[
            'dateframe'  => $dateframe,
            'data' => $data,
            'date'=>$date,
            'month_data'=>$month_data,
            'now_j' => $now_j
        ]);
    }
    //合同数据
    public function contract()
    {   
        return $this->fetch('contract');
    }
    //合同数据
    public function contract_json($limit='')
    {   
        $con = new CrmContract();
        $list = $con->with(['user'])->where('customer_id','in', my_select_auth())->order('end_time asc,id desc')->paginate($limit);
        $this->result($list);
    }
    //员工数据
    public function user()
    {   
        //时间 - 默认本月
        if(input('dateframe')){
            $dateframe = input('dateframe');
            $start_time = strtotime(substr($dateframe,0,10));
            $end_time = strtotime(substr($dateframe,-10))+86399;
        }else{
            $month = date('Y-m',time());
            $start_time = strtotime($month);//指定月份月初时间戳  
            $end_time = mktime(23, 59, 59, date('m', strtotime($month))+1, 00);//指定月份月末时间戳
            $dateframe = date('Y-m-d',$start_time).' - '.date('Y-m-d',$end_time);
        }
        return $this->fetch('user',[
            'dateframe'  => $dateframe
        ]);
    }
    //员工数据
    public function user_json($dateframe,$limit='')
    {   
        //时间 - 默认本月
        if($dateframe){
            $time['start_time'] = strtotime(substr($dateframe,0,10));
            $time['end_time'] = strtotime(substr($dateframe,-10))+86399;
        }
        //查询我的权限
        $user = User::where('id', UserId())->find();
        switch ($user['auth']){
            case "1":
                //获取所有相关USER_ID
                $user_id = [UserId()];
                break;
            case "2":
                //获取所有相关USER_ID
                $user_id = User::where('group_id',$user['group_id'])->column('id');;
                break;
            default :
                //获取所有相关USER_ID
                $user_id = User::where('status','1')->column('id');
                break;
        }
        $con = new User();
        $list = $con->with(['userGroup'])->withCount(['customer' => function($query)use($time) {
            $query->whereTime('create_time','between',[$time['start_time'],$time['end_time']]);
        },'track' => function($query)use($time) {
            $query->whereTime('create_time','between',[$time['start_time'],$time['end_time']]);
        },'contract' => function($query)use($time) {
            $query->whereTime('create_time','between',[$time['start_time'],$time['end_time']]);
        }])->
        withSum(['sales' => function($query)use($time) {
            $query->whereTime('create_time','between',[$time['start_time'],$time['end_time']]);
        }],'price')->where('id','in',$user_id)->paginate($limit);
        $this->result($list);
    }
    
    
}
